/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package br.edu.fatec.ads.util;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Expression;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;

/**
 *
 * @author Bruno Moura
 */
public class CriteriosUtil {

    private static CriteriosUtil util = null;
    public static String CRITERIOS_OBJETO = "criteriosObjeto";
    public static String CRITERIOS_IN = "criteriosIN";
    public static String CRITERIOS_IS_NULL = "criteriosNULL";
    public static String CRITERIOS_LIKE = "criteriosLike";
    public static String CRITERIOS_ORDEM_CRESCENTE = "criteriosOrdemCrescente";

    private CriteriosUtil() {
    }

    /**
     *
     * @return
     */
    public static CriteriosUtil getInstance(){
        if(util == null){
            util = new CriteriosUtil();
        }

        return util;
    }

    /**
     *
     * @param criterios
     * @return
     */
    public List<Criterion> montaCriterios(Map<String, Object> criterios){
       List<Criterion> criterion = new ArrayList<Criterion>();
       
       // Criterios
       if(criterios.containsKey(CriteriosUtil.CRITERIOS_OBJETO)) {
           Map<String, Object> auxiliar = (Map<String, Object>) criterios.get(CriteriosUtil.CRITERIOS_OBJETO);

           Iterator<String> iteratorCampos = auxiliar.keySet().iterator();
           String chave = null;

           while(iteratorCampos.hasNext()){
               chave = iteratorCampos.next();

               criterion.add(Restrictions.eq(chave, auxiliar.get(chave)));
           }
       }

       // Criterios IN
       if(criterios.containsKey(CriteriosUtil.CRITERIOS_IN)){
           Map<String, Object> auxiliar = (Map<String, Object>) criterios.get(CriteriosUtil.CRITERIOS_IN);

           Iterator<String> iteratorCampos = auxiliar.keySet().iterator();
           String chave = null;

           while(iteratorCampos.hasNext()){
               chave = iteratorCampos.next();

               criterion.add(Restrictions.in(chave, (List<Object>) auxiliar.get(chave)));
           }
       }

       if(criterios.containsKey(CriteriosUtil.CRITERIOS_LIKE)){
            Map<String, Object> auxiliar = (Map<String, Object>) criterios.get(CriteriosUtil.CRITERIOS_LIKE);

            Iterator<String> iteratorCampos = auxiliar.keySet().iterator();
            String chave = null;

            while(iteratorCampos.hasNext()){
                chave = iteratorCampos.next();

                criterion.add(Expression
                                .like(chave, 
                                    auxiliar.get(chave).toString(),
                                        MatchMode.ANYWHERE));
            }
       }

        // Criterios
        if(criterios.containsKey(CriteriosUtil.CRITERIOS_IS_NULL)) {
           List<String> campos = (List<String>) criterios.get(CriteriosUtil.CRITERIOS_IS_NULL);

           for(String campo : campos){
               criterion.add(Restrictions.isNull(campo));
           }
        }
       
       return criterion;
    }
}